Micron Document
Livres et Wikis | Archives | Info


Java syntax
part 7/23 Β· 86.1 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Control structures

Conditional statements

if statement

if statements in Java are similar to those in C and use the same syntax:

if (i == 3) {
doSomething();
}

if statement may include optional else block, in which case it becomes an if-then-else statement:

if (i == 3) {
doSomething();
} else {
doSomethingElse();
}

Like C, else-if construction does not involve any special keywords, it is formed as a sequence of separate if-then-else statements:

if (i == 3) {
doSomething();
} else if (i == 2) {
doSomethingElse();
} else {
doSomethingDifferent();
}

Also, a ?: operator can be used in place of simple if statement, for example

int a = 1;
int b = 2;
int minVal = (a < b) ? a : b;

switch statement

Switch statements in Java can use byte, short, char, and int (not long) primitive data types or their corresponding wrapper types. Starting with J2SE 5.0, it is possible to use enum types. Starting with Java SE 7, it is possible to use Strings.cite-ref-2[2] Other reference types cannot be used in switch statements.

Possible values are listed using case labels. These labels in Java may contain only constants (including enum constants and string constants). Execution will start after the label corresponding to the expression inside the brackets. An optional default label may be present to declare that the code following it will be executed if none of the case labels correspond to the expression.

Code for each label ends with the break keyword. It is possible to omit it causing the execution to proceed to the next label, however, a warning will usually be reported during compilation.

switch (ch) {
case 'A':
doSomething(); // Triggered if ch == 'A'
break;
case 'B':
case 'C':
doSomethingElse(); // Triggered if ch == 'B' or ch == 'C'
break;
default:
doSomethingDifferent(); // Triggered in any other case
break;
}

switch expressions

Since Java 14 it has become possible to use switch expressions, which use the new arrow syntax:

var result = switch (ch) {
case 'A' -> Result.GREAT;
case 'B', 'C' -> Result.FINE;
default -> throw new ThisIsNoGoodException();
};

Alternatively, there is a possibility to express the same with the yield statement, although it is recommended to prefer the arrow syntax because it avoids the problem of accidental fall throughs.

var result = switch (ch) {
case 'A':
yield Result.GREAT;
case 'B':
case 'C':
yield Result.FINE;
default:
throw new ThisIsNoGoodException();
};

Iteration statements


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────